#include "../mqserver/mq_message.hpp"
#include <gtest/gtest.h>

mymq::MessageManager::ptr mmp;

class MessageTest : public testing::Environment
{
    public:
        virtual void SetUp() override
        {
            mmp = std::make_shared<mymq::MessageManager>("./data/message/");
            mmp->InitQueueMessage("queue1");
        }

        virtual void TearDown() override
        {
            mmp->clear();
        }
};

// 新增消息测试
TEST(message_test, insert_test) {
    mymq::BasicProperties properties;
    properties.set_id(mymq::UUIDHelper::uuid());
    properties.set_delivery_mode(mymq::DeliveryMode::DURABLE);
    properties.set_routing_key("news.music.pop");
    mmp->insert("queue1", &properties, "Hello World-1", mymq::DeliveryMode::DURABLE);
    mmp->insert("queue1", nullptr, "Hello World-2", mymq::DeliveryMode::DURABLE);
    mmp->insert("queue1", nullptr, "Hello World-3", mymq::DeliveryMode::DURABLE);
    mmp->insert("queue1", nullptr, "Hello World-4", mymq::DeliveryMode::DURABLE);
    mmp->insert("queue1", nullptr, "Hello World-5",mymq::DeliveryMode::UNDURABLE);
    ASSERT_EQ(mmp->push_count("queue1"), 5);
    ASSERT_EQ(mmp->total_count("queue1"), 4);
    ASSERT_EQ(mmp->durable_count("queue1"), 4);
    ASSERT_EQ(mmp->waitack_count("queue1"), 0);
}

// // 恢复历史数据测试
// TEST(message_test, recovery_test)
// {
//     ASSERT_EQ(mmp->push_count("queue1"),4);
//     mymq::MessagePtr msg1 = mmp->front("queue1");
//     ASSERT_NE(msg1.get(),nullptr);
//     ASSERT_EQ(msg1->payload().body(),std::string("Hello World-1"));
//     ASSERT_EQ(mmp->push_count("queue1"), 3);
//     ASSERT_EQ(mmp->waitack_count("queue1"), 1);

//     mymq::MessagePtr msg2 = mmp->front("queue1");
//     ASSERT_EQ(msg2->payload().body(),std::string("Hello World-2"));
//     ASSERT_EQ(mmp->push_count("queue1"), 2);
//     ASSERT_EQ(mmp->waitack_count("queue1"), 2);

//     mymq::MessagePtr msg3 = mmp->front("queue1");
//     ASSERT_EQ(msg3->payload().body(),std::string("Hello World-3"));
//     ASSERT_EQ(mmp->push_count("queue1"), 1);
//     ASSERT_EQ(mmp->waitack_count("queue1"), 3);

//     mymq::MessagePtr msg4 = mmp->front("queue1");
//     ASSERT_EQ(msg4->payload().body(),std::string("Hello World-4"));
//     ASSERT_EQ(mmp->push_count("queue1"), 0);
//     ASSERT_EQ(mmp->waitack_count("queue1"), 4);
// }

TEST(message_test, select_test)
{
    mymq::MessagePtr msg1 = mmp->front("queue1");
    ASSERT_NE(msg1.get(),nullptr);
    ASSERT_EQ(msg1->payload().body(),std::string("Hello World-1"));
    ASSERT_EQ(mmp->push_count("queue1"), 4);
    ASSERT_EQ(mmp->waitack_count("queue1"), 1);

    mymq::MessagePtr msg2 = mmp->front("queue1");
    ASSERT_EQ(msg2->payload().body(),std::string("Hello World-2"));
    ASSERT_EQ(mmp->push_count("queue1"), 3);
    ASSERT_EQ(mmp->waitack_count("queue1"), 2);

    mymq::MessagePtr msg3 = mmp->front("queue1");
    ASSERT_EQ(msg3->payload().body(),std::string("Hello World-3"));
    ASSERT_EQ(mmp->push_count("queue1"), 2);
    ASSERT_EQ(mmp->waitack_count("queue1"), 3);

    mymq::MessagePtr msg4 = mmp->front("queue1");
    ASSERT_EQ(msg4->payload().body(),std::string("Hello World-4"));
    ASSERT_EQ(mmp->push_count("queue1"), 1);
    ASSERT_EQ(mmp->waitack_count("queue1"), 4);

    mymq::MessagePtr msg5 = mmp->front("queue1");
    ASSERT_NE(msg5.get(),nullptr);
    ASSERT_EQ(msg5->payload().body(),std::string("Hello World-5"));
    ASSERT_EQ(mmp->push_count("queue1"), 0);
    ASSERT_EQ(mmp->waitack_count("queue1"), 5);
}

// 删除消息测试：确认一条消息，查看持久化消息数量，待确认消息数量
// TEST(message_test, delete_test) {
//     ASSERT_EQ(mmp->push_count("queue1"), 5);
//     mymq::MessagePtr msg1 = mmp->front("queue1");
//     ASSERT_NE(msg1.get(), nullptr);
//     ASSERT_EQ(msg1->payload().body(), std::string("Hello World-1"));
//     ASSERT_EQ(mmp->push_count("queue1"), 4);
//     ASSERT_EQ(mmp->waitack_count("queue1"), 1);
//     mmp->ack("queue1", msg1->payload().properties().id());
//     ASSERT_EQ(mmp->waitack_count("queue1"), 0);
//     ASSERT_EQ(mmp->durable_count("queue1"), 3);
//     ASSERT_EQ(mmp->total_count("queue1"), 4);
// }

//销毁测试
TEST(message_test, clear) {
    mmp->DestroyQueueMessage("queue1");
}

int main(int argc,char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new MessageTest);
    int ret = RUN_ALL_TESTS();
    (void)ret;
    return 0;
}